lex file-
char [A-Za-z]
digit [0-9]
%%
if                 return IF;
then                 return THEN;
else                 return ELSE;
{char}({char}|{digit})*    return ID;
{digit}+             {yylval=atoi(yytext); return NUM;}
[ \t]                 ;
\n                yyterminate();
.                 return yytext[0];
%%
----------------------------------------------------
yacc file-


%token IF THEN ELSE ID NUM
%right '='
%left '+' '-'
%left '*' '/'
%left UMINUS
%%
S : IF '(' F ')'{fun1();} THEN F ';'{fun2();} ELSE F ';'{fun3();}
  ;
F : I '='{push();} F{code_ass();}
  | F '+'{push();} F{code();}
  | F '-'{push();} F{code();}
  | F '<'{push();} F{code();}
  | F '>'{push();} F{code();}
  | F '*'{push();} F{code();}
  | F '/'{push();} F{code();}
  | '(' F ')';
  | '-'{push();} F{code_umin();} %prec UMINUS
  | I
  | NUM{push();}
  ;
I : ID {push();}
  ;
%%

#include "lex.yy.c"
#include<string.h>
char st[100][10];
int top=0;
char i_[2]="0";
char tmp[2]="t";

int label[20];
int lnum=0;
int ltop=0;

main()
 {
 printf("Enter the expression : ");
 yyparse();

 }
void yyerror (char const *s) {
   fprintf (stderr, "%s\n", s);
 }
push()
 {
  strcpy(st[++top],yytext);
 }

code()
 {
 strcpy(tmp,"t");
 strcat(tmp,i_);
  printf("%s = %s %s %s\n",tmp,st[top-2],st[top-1],st[top]);
  top-=2;
 strcpy(st[top],tmp);
 i_[0]++;
 }

code_umin()
 {
 strcpy(tmp,"t");
 strcat(tmp,i_);
 printf("%s = -%s\n",tmp,st[top]);
 top--;
 strcpy(st[top],tmp);
 i_[0]++;
 }

code_ass()
 {
 printf("%s = %s\n",st[top-2],st[top]);
 top-=2;
 }

fun1()
{
 lnum++;
 strcpy(tmp,"t");
 strcat(tmp,i_);
 printf("%s = not %s\n",tmp,st[top]);
 printf("if %s goto L%d\n",tmp,lnum);
 i_[0]++;
 label[++ltop]=lnum;
}

fun2()
{
int x;
lnum++;
x=label[ltop--];
printf("goto L%d\n",lnum);
printf("L%d: \n",x);
label[++ltop]=lnum;
}

fun3()
{
int y;
y=label[ltop--];
printf("L%d: \n",y);
}




-------------------------------------------------
Output-
mohit@mohit-PC ~/Sem 8/CD_Lab /Parser $ lex if.l
mohit@mohit-PC ~/Sem 8/CD_Lab /Parser $ yacc -d if.y
mohit@mohit-PC ~/Sem 8/CD_Lab /Parser $ gcc y.tab.c -ll -lm
mohit@mohit-PC ~/Sem 8/CD_Lab /Parser $ ./a.out
Enter the expression : if(a>10)then a=100 ;else b=90;
t0 = a > 10
t1 = not t0
if t1 goto L1

a = 100
goto L2
L1: 
b = 90
L2: 
mohit@mohit-PC ~/Sem 8/CD_Lab /Parser $ 